<template>
  <div class="home-container">
    <div class="banner">
      <h1 class="banner-title">自制留言板</h1>
    </div>

    <div>
      <el-card class="cardbg">
        <div class="firstmenu">
          <el-col :span="8">
            <el-input
              type="text"
              placeholder="请输入你的名字"
              v-model="messageBoard.author"
              maxlength="16"
              show-word-limit
              width="40%"
            >
            </el-input>
          </el-col>
          <el-col :span="8" :offset="4">
            <el-input
              type="text"
              placeholder="请输入你的头像链接"
              v-model="messageBoard.avater"
              width="40%"
              show-word-limit
            >
            </el-input>
          </el-col>
        </div>

        <el-input
          type="textarea"
          placeholder="请输入留言"
          v-model="messageBoard.message"
          show-word-limit
          :rows="6"
        >
        </el-input>
        <div style="margin: 20px 0"></div>
        <el-button type="primary" plain @click="postMessage">留言</el-button>
        <el-divider></el-divider>
        <el-table :data="messages" height="260" border style="width: 100%">
          <el-table-column prop="avatar" label="头像链接"> </el-table-column>
          <el-table-column prop="author" label="名字"> </el-table-column>
          <el-table-column prop="message" label="留言"> </el-table-column>
          <el-table-column prop="time" label="时间" width="160"> </el-table-column>
        </el-table>

        <el-divider></el-divider>

        <div class="talk-item" v-for="item of messages" :key="item.id">
          <div class="user-info-wrapper">
            <el-avatar size="36" class="user-avatar">
              <img :src="item.avatar" alt="" />
            </el-avatar>
            <div class="user-detail-wrapper">
              <div class="user-nickname">
                {{ item.author }}
              </div>
              <div class="time">
                {{ item.time }}
              </div>
              <div class="talk-content" v-html="item.message"></div>
            </div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Messageown",
  data() {
    return {
      messageBoard: {
        author: "",
        message: "",
        avater: "",
      },
      messages: [],
    };
  },
  methods: {
    postMessage() {
      if (this.messageBoard.author === "") {
        this.$message.error("请输入昵称");
        return;
      }
      if (this.messageBoard.message === "") {
        this.$message.error("请输入留言");
        return;
      }

      axios
        .get(
          "http://localhost:9000/api/v1/postMessage?author=" +
            this.messageBoard.author +
            "&message=" +
            this.messageBoard.message +
            "&avatar=" +
            this.messageBoard.avater
        )
        .then((t) => {
          if (t.data === "success") {
            this.$message({
              message: "留言成功！",
              type: "success",
            });
            location.reload();
          } else {
            this.$message.error("留言失败。");
          }
        });
    },
  },
  created() {
    axios.get("http://localhost:9000/api/v1/getMessages").then((t) => {
      this.messages = t.data;
      // console.log(this.messages);
    });
  },
};
</script>

<style scoped>
.firstmenu {
  margin-bottom: 20px;
  height: 40px;
}
.cardbg {
  /* background-color: #eee; */
  opacity: 0.8;
  height: 100%;
  /* display: flex;
  flex-direction: column; */
  position: relative;
  animation: main 1s;
  max-width: 951px;
  padding: 50px 40px;
  margin: 26px auto 17px auto;
}

.home-container {
  z-index: -9;
  /* height: 100vh; */
  width: 100%;
  /* overflow: auto; */
  /* overflow-y: scroll; */
  /* position: absolute; */
  /* top: -60px; */
}
.banner {
  /* position: absolute; */
  background-color: #49b1f5 !important;
  top: -60px;
  left: 0;
  right: 0;
  height: 320px;
  animation: header-effect 1s;
  opacity: 0.8;
  background: url("http://img.xiyblog.cn/img/21.jpg") center center / cover no-repeat;
}
.banner-title {
  animation: title-scale 1s;
  position: relative;
  top: 6.5rem;
  padding: 0 0.5rem;
  width: 100%;
  font-size: 2.5rem;
  text-align: center;
  color: #eee;
}

.talk-item:not(:first-child){
  margin-top: 20px;
}
.talk-item{
  padding: 16px 20px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.1);
  box-shadow: 0 3px 8px 6px rgb(7 17 27 / 6%);
  transition: all 0.3s ease 0s;
}
.talk-item:hover{
  box-shadow: 0 5px 10px 8px rgb(7 17 27 / 16%);
  transform: translateY(-3px);
}
.user-info-wrapper{
  width: 100%;
  display: flex;
}
.user-avatar{
  border-radius: 50%;
  transition: all 0.5s;
}
.user-avatar:hover{
  transform: rotate(360deg);
}
.user-detail-wrapper{
  flex: 1;
  margin-left: 10px;
  width: 0;
}
.user-nickname{
  margin-left: 4px;
}
.time{
  color: #999;
  margin-top: 2px;
  font-size: 12px;
}
.talk-content{
  margin-top: 8px;
  font-size: 14px;
  white-space: pre-line;
  word-wrap: break-word;
  word-break: break-all;
}
</style>
